import {Utils} from '../utils/Utils';
import {AjaxMsg} from '../utils/AjaxMsg';

import {MobileNumInput} from './MobileNumInput';
import {SmsVerifyCodeInput} from './SmsVerifyCodeInput';
import {UserNameInput} from './UserNameInput';
import {PassWordInput} from './PassWordInput';
import {RememberMeInput} from './RememberMeInput';
import {SendSmsButton} from './SendSmsButton';
import {LoginSubmitButton} from './LoginSubmitButton';

export class LoginForm {
    constructor(app){
        this.app = app;
        this.api = app.api;
        this.dom = app.dom;

        this.loginMode = app.loginMode;
        this.shouldUseCaptcha = false;
        this.baseFormData = {};

        //手机号登录框
        this.smsVerifyCodeInput = new SmsVerifyCodeInput(this, this.dom.find('input[name="smsVerifyCode"]'));
        this.mobileNumInput = new MobileNumInput(this, this.dom.find('input[name="mobileNum"]'));
        this.sendSmsButton = new SendSmsButton(this, this.dom.find('#sendSms'));

        //用户名密码登录框
        this.userNameInput = new UserNameInput(this, this.dom.find('input[name="username"]'));
        this.passWordInput = new PassWordInput(this, this.dom.find('input[name="password"]'));

        //记住密码框
        this.rememberMeInput = new RememberMeInput(this, this.dom.find('#rememberMe'));

        //登录按钮
        this.submitButton = new LoginSubmitButton(this, this.dom.find('#submit'));

        this.initialize();
    }

    initialize(){
        this.bindSwitchModeButton();
    }

    bindSwitchModeButton(){
        this.dom.find('#loginModeAccount').on('click', () => {
            this.app.setLoginMode('account');
            return false;
        });
        this.dom.find('#loginModeMobile').on('click', () => {
            this.app.setLoginMode('mobile');
            return false;
        });
    }

    disableLogin(message = null){
        this.submitButton.disable(message);
    }

    enableLogin(){
        this.submitButton.enable();
    }

    onChangeUser(){
        if(this.submitButton._disabled){
            this.enableLogin();
        }
    }

    getFields(){
        let fields = {};
        if(this.loginMode === 'mobile'){
            fields = {
                mobileNum: this.mobileNumInput,
                smsVerifyCode: this.smsVerifyCodeInput,
            };
        } else if(this.loginMode === 'account'){
            fields = {
                username: this.userNameInput,
                password: this.passWordInput,
            };
        }
        fields.remember = this.rememberMeInput;
        return fields;
    }

    validate(){
        let fields = this.getFields();
        let hasError = false;
        for(let key in fields){
            let one = fields[key];
            let result = true;
            if('baseValidate' in one){
                result = result && one.baseValidate();
            }
            if('validate' in one){
                result = result && one.validate();
            }
            if(!result){
                hasError = true;
            }
        }
        return !hasError;
    }

    getFormData(){
        let fields = this.getFields();
        let formData = {};
        for(let key in fields){
            let one = fields[key];
            if('getValue' in one && typeof one.getValue === 'function'){ //自定义方法
                let value = one.getValue();
                if(typeof value === 'string' && value.length > 0){
                    formData[key] = value;
                }
            } else if('val' in one && typeof one.val === 'function'){ //jQuery原生方法
                let value = one.val();
                if(typeof value === 'string' && value.length > 0){
                    formData[key] = value;
                }
            }
        }
        return formData;
    }

    doLogin(){
        if(this.validate()){ //本地检测登录的信息
            if(this.shouldUseCaptcha){
                this.app.captcha.verify().then((captchaData) => {
                    this.baseFormData = Utils.cloneObject(captchaData); //设置验证码信息
                    this.sendLoginRequest(this.baseFormData);
                }).catch((type) => {

                });
            } else {
                this.sendLoginRequest();
            }
        }
    }

    sendLoginRequest(baseData = {}){
        this.disableLogin('正在登录……');
        this.api.login(Utils.mergeObject(baseData, this.getFormData())).then((msg) => {
            let ajaxMsg = new AjaxMsg(msg);
            ajaxMsg.showFocus(this);
            ajaxMsg.showMsg();

            if(ajaxMsg.status === 1){ //登录成功
                this.form.disableLogin('登录成功');
                if(ajaxMsg.data.shouldUpgrade){ //需要升级账号

                } else {
                    
                }
            } else if(ajaxMsg.status === 0){ //有错误
                this.enableLogin;
                if(ajaxMsg.data.shouldUseCaptcha && !this.shouldUseCaptcha){ //需要使用验证码
                    this.shouldUseCaptcha = true;
                    this.doLogin();
                }
            } else if(ajaxMsg.status === -1){ //没有注册
                this.form.disableLogin('请注册账号');
            } else {
                this.enableLogin();
            }
        });
    }

    upgradeAccount(){

    }
}